/**
 * Created by IntelliJ IDEA.
 * User: scotthatcher
 * Date: Mar 24, 2010
 * Time: 1:32:28 PM
 * To change this template use File | Settings | File Templates.
 */
import acm.program.*;

public class CaesarCipher extends ConsoleProgram {

    public void run(){
        println("This program uses a Ceasar cipher for encryption.");
        int key = readInt("Enter encryption key: ");
        String plaintext = readLine("Plaintext: ");
        String ciphertext = encryptCeasar(plaintext, key);
        println("Ciphertext: " + ciphertext);

    }
    private char encryptChar(char ch, int key){
        if (Character.isUpperCase(ch)){
            return( (char)('A' + ((ch - 'A' + key) % 26)) );
        }
        return ch;
        
    }

    private String encryptCeasar(String str, int key){
        if (key < 0) {
            key = 26 - (-key % 26);
        }

        String result = "";
        for(int i=0; i < str.length(); i++){
            char ch = str.charAt(i);
            result += encryptChar(ch, key);
            
        }
        return result;
        


    }
}
